<template>
  <div class="box" style="height:500px">
    <div class="xtx-slider" @mouseenter="showFn" @mouseleave="hideFn">
      <!-- 图片列表 -->
      <ul class="slider-body">
        <!--
          fade: 当fade类名存在 当前图片就显示 不存在就不显示
         -->
        <li
          class="slider-item"
          v-for="(item,index) in sliders"
          :key="item.id"
          :class="{ fade: curId === index }"
          @mouseenter="startAuto"
          @mouseleave="leaveFn"
        >
          <img :src="item.imgUrl" alt="" />
        </li>
      </ul>
      <!-- 圆圈切换按钮 -->
      <div class="slider-indicator">
        <span :class="{ active: curId === index }"
          v-for="(item,index) in sliders"
          :key="index"
          @click="curId = index"></span>
      </div>
      <a class="prev"  v-if="showFlag" href="javascript:;" @click="prevFn"> <i class="iconfont icon-angle-left"></i></a>
      <a class="next" v-if="showFlag" href="javascript:;"  @click="nextFn"><i class="iconfont icon-angle-right"></i></a>
    </div>
  </div>
</template>

<script>
/**
 * 目标：点击圆圈按钮 实现对应图片的切换
 * 思路：
 *   1. 图片和圆圈按钮数量是一样的 下标值是对应的
 *   2. 记录一下当前点击的是哪一项
 *   3. 需要根据记录下来的下标值 去配合:class 控制fade这个类名是否应该显示
 */
import { ref, onMounted, onUnmounted } from 'vue'
export default {
  name: 'XtxSlider',
  props: {
    // 数据列表
    sliders: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 是否自动轮播
    autoPlay: {
      type: Boolean,
      default: true
    }
  },
  setup (props) {
    const curId = ref(0)
    const showFlag = ref(false)
    let timer = null
    // 自动播放
    function autoPlay () {
      timer = setInterval(() => {
        if (!props.autoPlay) {
          return false
        }
        curId.value++
        if (curId.value === props.sliders.length) {
          curId.value = 0
        }
      }, 1500)
    }
    onMounted(() => {
      autoPlay()
    })
    // 清除定时器
    function stopAuto () {
      clearInterval(timer)
    }
    function startAuto () {
      stopAuto()
    }
    function leaveFn () {
      autoPlay()
    }
    onUnmounted(() => {
      clearInterval(timer)
    })
    // 鼠标进入 显示 上一页
    function showFn () {
      showFlag.value = true
    }
    function hideFn () {
      showFlag.value = false
    }
    // 上一页
    function prevFn () {
      curId.value--
      if (curId.value === -1) {
        curId.value = props.sliders.length - 1
      }
    }
    // 下一页
    function nextFn () {
      curId.value++
      if (curId.value === props.sliders.length) {
        curId.value = 0
      }
    }
    return {
      curId,
      startAuto,
      leaveFn,
      showFlag,
      showFn,
      hideFn,
      prevFn,
      nextFn
    }
  }
}
</script>

<style scoped lang='less'>
.xtx-slider {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .slider {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .slider-btn {
      opacity: 1;
    }
  }
  .prev {
    width: 44px;
    height: 44px;
    background: rgba(0,0,0,.2);
    color: #fff;
    border-radius: 50%;
    line-height: 44px;
    position: absolute;
    top: 228px;
    left: 260px;
    z-index: 2;
   text-align: center;
   opacity: 1;
   transition: all .5s;
  }
  .next{
    width: 44px;
    height: 44px;
    background: rgba(0,0,0,.2);
    color: #fff;
    border-radius: 50%;
    line-height: 44px;
    position: absolute;
    top: 228px;
    right: 20px;
    z-index: 2;
   text-align: center;
   opacity: 1;
   transition: all .5s;
  }
}
</style>
